<!-- 主办会议 -->
<template>
  <div class="module_">
    <div class="flex-space-between mb8">
      <search @clearSearch='clearSearch' :status='true'>
        <el-form autocomplete="off"  :model="pageparm" class="search_box">
        <el-form-item v-for="(v, i) in headerList" :key="i + v.prop">
          <inputSearch v-if="v.prop === 'departments_id' && adminShow" ref="search_departments" type="departments" @onChange='search_departments'  />
          <!-- <el-date-picker
            v-else-if="v.prop === 'attendance_time'"
            size="medium"
            v-model="pageparm[v.prop]"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="选择日期时间">
          </el-date-picker> -->
          <span v-else-if="v.prop === 'attendance_time'"></span>
          <el-input
            clearable
            v-else
            size="medium"
            :type="v.type"
            autosize
            v-model.trim="pageparm[v.prop]"
            :placeholder="'请输入' + v.label + '搜索'"/>
        </el-form-item>
        </el-form>
      </search>
      <div class="flex">
        <el-button class="mr8" size="medium" type="primary" @click="handle(null,{},3)">新增会议</el-button>
      </div>
    </div>
    <el-table
      id="out-table"
      ref="table"
      :data="tableData"
      class="flex-1"
      height="100%" size="medium" 
      border 
      highlight-current-row
      v-loading="tableLoading"
      stripe
      @sort-change="tableSortChange">  
      <el-table-column
        align="center"
        v-for="(item,i) in headerList"
        :key="item.prop"
        :prop="item.prop"
        :label="item.label"
        :width="item.width"
        :fixed="i === 0 ? true: false"
        show-overflow-tooltip
        :sortable="item.noSortable ? false:'custom'">
        <template slot-scope="scope">
          <template v-if="item.prop === 'departments_id'">
            {{ scope.row.department?scope.row.department.name:'' }}
          </template>
          <template v-else>
           {{ scope.row[item.prop] }}
          </template>
        </template>
      </el-table-column>
      <el-table-column  align="center" label="审核状态" width="250">
        <template slot-scope="scope">
          <div class="flex-center">
            <span class="mr8">{{echo(scope.row.review_status,reviewStatus)}}</span>
            <el-button type="primary" @click="examine_btns(scope.row,1)" size="medium" v-if="scope.row.review_status == 0" class="ml8">审核</el-button>
            <el-button type="success" @click="examine_btns(scope.row,2)" size="medium" v-else-if="scope.row.review_status == 1" class="ml8">查看详情</el-button>
            <el-button type="danger" @click="examine_btns(scope.row,3)" size="medium" v-else class="ml8">查看原因</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-tooltip  effect="dark" content="修改信息" placement="bottom-end">
            <el-button size="medium"  @click="handle(scope.$index, scope.row,2)" icon="el-icon-edit" circle></el-button>
          </el-tooltip>
          <el-tooltip  effect="dark" content="删除" placement="bottom-end">
            <el-button size="medium" type="danger" @click="deleteUser(scope.$index, scope.row)" icon="el-icon-delete" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <el-dialog :visible.sync="editFormVisible" width="600px" @close='dialogClose'
    :close-on-click-modal='false'
    :close-on-press-escape='false'>
      <el-form autocomplete="off"  label-width="130px" :model="editForm" class="form">
        <el-form-item
          v-for="(v, i) in editHeaderList"
          :key="i"
          :label="v.label"
          :class="v.required ? 'tip' : ''">
          <inputSearch v-if="v.prop === 'departments_id'" ref="departmentSearch" type="departments" @onChange='departmentsChange' :departmentsId='departmentsId'  />
          <el-date-picker
            v-else-if="v.prop === 'attendance_time'"
            size="medium"
            v-model="editForm[v.prop]"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="选择日期时间">
          </el-date-picker>
          <el-input
            v-else
            clearable
            size="medium"
            :type="v.type"
            autosize
            v-model="editForm[v.prop]"
            @mousewheel.native.prevent
            :placeholder="'请输入' + v.label"/>
        </el-form-item>
      </el-form> 
      <div slot="footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit" :loading='btnLoading'>确 定</el-button>
      </div>
    </el-dialog>
    
    <!-- 审核 -->
    <examine ref="examine" @submit='examineSubmit' :item='examine_item' />   

  </div>
</template>

<script>

// import {
//     Apihostmeetings,ApihostmeetingsAdd,ApihostmeetingsPut,ApihostmeetingsDel,ApihostmeetingsCheck
// } from '@/api'
import {reviewStatus} from "@/assets/js"
import { debounce,adminShow } from "@/utils/index";
import {headerList} from '../js/hostConference'
export default {
  computed:{
      editHeaderList(){
          let list = [...headerList]
          return list.sort((a,b) => b.required - a.required)
      }
  },
  data(){
    return{
      adminShow:adminShow(),
      reviewStatus,
      tableData:[], 
      editFormVisible: false, 
      editForm: {},  //修改或者查看详情

      pageparm: {
        page: 1,
        perPage: 20,
        total: null,
        sort_str: null, 
        sort_order: 'desc' ,
      },
      tableLoading: false,

      btnLoading: false,

      headerList,
      rules:['name','organizer','departments_id','attendance_time'],

      departmentsId:null,
      examine_id: null,  //审核id
      examine_item:{
        view: false
      }
    }
  },
  mounted(){
    this.getList()
  },
  methods:{
    //审核
    examineSubmit(options){
      options.id = this.examine_id
      ApihostmeetingsCheck(options,options.id).then(res => {
        this.getList()
      }).catch(()=>{
        this.$message.error('服务器错误，请重新提交');   
      })
    },
    examine_btns(item,i){
      this.examine_id = item.id
      if(i > 1){
        this.examine_item.review_status = item.review_status
        this.examine_item.review_comment = item.review_comment
        this.examine_item.view = true
      }else{
        // this.examine_item.review_status = null
        // this.examine_item.review_comment = null
        this.examine_item.view = false
      }
      this.$refs.examine.examineShow = true

    },
    echo(i,arr){
      let label
      for (const item of arr) {
        if(item.value === i){
          label = item.label
          break;
        }
      }
      return label
    },
    tableSortChange(column){
      if (column.order === "descending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "desc";
      } else if (column.order === "ascending") {
        this.pageparm.sort_str = column.prop;
        this.pageparm.sort_order = "asc";
      }else{
        this.pageparm.sort_str = null;
        this.pageparm.sort_order = null;
      }
      this.getList()
    },
    //搜索
    clearSearch(){
      if(this.adminShow)this.$refs.search_departments[0].clear()
    },
    search_departments(id){
      this.pageparm.departments_id = id
      this.pageparm.page = 1
      this.getList()
    }, 
    //分页
    callFather(pageparm){
      this.pageparm.page = pageparm.page
      this.pageparm.perPage = pageparm.perPage
      this.getList()
    },
    getList(){
      this.tableLoading = true
      Apihostmeetings(this.pageparm).then(({data,page}) => {
        this.tableData = data || []
        this.pageparm.total = page.total
        this.tableLoading = false
      }).catch(()=>{
        this.tableLoading = false
      })
    },
    handle(index,value,i){
      this.editForm = {...value}
      this.editFormVisible = true
      if(i == 2){
        this.$nextTick(()=>{
          this.$refs.departmentSearch[0].departments_id = value.departments_id
        })
      }
    },
    departmentsChange(id){
      this.editForm.departments_id = id
    },
    dialogClose(){
      this.$refs.departmentSearch[0].clear()
    },
    deleteUser(index,value){
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({
            lock: true,
            text: '加载中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          ApihostmeetingsDel(value.id).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          }).finally(()=>{
            loading.close()
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    submit : debounce(function(){   //提交  修改或新增
      const options = {...this.editForm}
      // options.coefficient = this.editForm.coefficient || 1
      for (const key of this.rules) {
        if(!options[key] && options[key] !== 0){
          this.$message({
            type: 'error',
            message: '必填信息请填写完整！'
          });
          return
        }
      }
      this.btnLoading = true
      if(options.id){   //有ID就是修改
        ApihostmeetingsPut(options,options.id).then(res => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.editFormVisible = false
          this.getList()
        }).catch(()=>{
          this.$message({
            type: 'error',
            message: '修改失败!'
          });
        }).finally(()=>{
            this.btnLoading = false
        });
      }else{
        ApihostmeetingsAdd(options).then(res => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          });
          this.editFormVisible = false
          this.getList()
        }).catch(()=>{
          this.$message({
            type: 'error',
            message: '添加失败!'
          });
        }).finally(()=>{
            this.btnLoading = false
        });
      }
    },800,true)
  },

}
</script>
<style lang='less' scoped>
</style>